<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="记录编号" prop="code" v-hasRole="['admin', 'root']">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入记录编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程编号" prop="classcode">
        <el-input
          v-model="queryParams.classcode"
          placeholder="请输入课程编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="classname">
        <el-input
          v-model="queryParams.classname"
          placeholder="请输入课程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师号" prop="tno">
        <el-input
          v-model="queryParams.tno"
          placeholder="请输入教师号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师姓名" prop="tname">
        <el-input
          v-model="queryParams.tname"
          placeholder="请输入教师姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="sno">
        <el-input
          v-model="queryParams.sno"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="sname">
        <el-input
          v-model="queryParams.sname"
          placeholder="请输入学生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考勤时间" prop="starttime">
        <el-date-picker clearable size="small"
          v-model="queryParams.starttime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择考勤时间" style="width: 150px;">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="考勤地点" prop="addressid">
        <el-select v-model="queryParams.addressid" placeholder="请选择考勤地点" clearable size="small">
          <el-option
            v-for="dict in addressOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange" max-height="534">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录id" align="center" prop="id" v-if="checkRole(['admin','root'])"/>
      <el-table-column label="记录编号" align="center" prop="code" v-if="checkRole(['admin','root'])"/>
      <el-table-column label="课程编号" align="center" prop="classcode" />
      <el-table-column label="课程名称" align="center" prop="classname" width="200"/>
      <el-table-column label="考勤地点" align="center" prop="addressid" :formatter="addressFormat"/>
      <el-table-column label="教师号" align="center" prop="tno" />
      <el-table-column label="教师姓名" align="center" prop="tname" />
      <el-table-column label="学号" align="center" prop="sno" width="100"/>
      <el-table-column label="学生姓名" align="center" prop="sname" />
      <el-table-column label="考勤开始时间" align="center" prop="starttime" width="180" />
      <el-table-column label="考勤结束时间" align="center" prop="endtime" width="180" />      
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" min-width="125" class-name="small-padding fixed-width" fixed="right" v-if="checkRole(['admin','root','teacher'])">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考勤记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/basic/record";
import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
import { listAddress} from "@/api/basic/address";

export default {
  name: "Record",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //同样是选中数组
      codes:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      //考勤地点字典
      addressOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        classcode: null,
        addressid: null,
        tno: null,
        tname: null,
        sno: null,
        sname: null,
        starttime: null,
        classname: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "记录编号不能为空", trigger: "blur" }
        ],
        classcode: [
          { required: true, message: "课程编号不能为空", trigger: "blur" }
        ],
        addressid: [
          { required: true, message: "考勤地点不能为空", trigger: "blur" }
        ],
        tno: [
          { required: true, message: "教师号不能为空", trigger: "blur" }
        ],
        tname: [
          { required: true, message: "教师姓名不能为空", trigger: "blur" }
        ],
        sno: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        sname: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        starttime: [
          { required: true, message: "考勤开始时间不能为空", trigger: "blur" }
        ],
        endtime: [
          { required: true, message: "考勤结束时间不能为空", trigger: "blur" }
        ],
        classname: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getAddressList();
    this.getDicts("kaoqin_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    checkPermi,
    checkRole,
    /**获取考勤地点字典信息 */
    getAddressList(){
      listAddress({pageNum: 1,pageSize: 1000}).then(response => {
        for (const iterator of response.rows) {
          this.addressOptions[this.addressOptions.length] = {
            dictValue:iterator.id,
            dictLabel:iterator.address
          }
        }
      }).then(()=>{
        this.getList();
      });
    },
    /** 查询考勤记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //考勤地点字典翻译
    addressFormat(row, column) {
      return this.selectDictLabel(this.addressOptions, row.addressid);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        classcode: null,
        addressid: "0",
        tno: null,
        tname: null,
        sno: null,
        sname: null,
        starttime: null,
        endtime: null,
        classname: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.codes = selection.map(item => item.code)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        for (const iterator of this.statusOptions) {
          if(this.form.status==iterator.dictValue){
            this.form.status = iterator.dictLabel
          }
        }
        this.open = true;
        this.title = "修改学生考勤状态";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            for (const iterator of this.statusOptions) {
              if(this.form.status==iterator.dictLabel){
                this.form.status = iterator.dictValue
              }
            }
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const codes = row.code || this.codes
      this.$confirm('是否确认删除考勤记录编号为"' + codes + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考勤记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style lang="css">

.el-input--small .el-input__inner{
  width: 150px;
}
</style>